package com.qiu.DitingPlatform.exception;

import com.qiu.DitingPlatform.common.BaseResponse;
import com.qiu.DitingPlatform.common.ErrorCode;
import com.qiu.DitingPlatform.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 运行时异常处理器
     */
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException:", e.getMessage(), e);
        return ResultUtils.error(ErrorCode.SYS_ERROR, e.getMessage(), "系统异常");
    }

    /**
     * 业务异常处理器
     */
    @ExceptionHandler(BusinessException.class)
    public BaseResponse BusinessExceptionHandler(BusinessException e) {
        log.error("BusinessException:", e);
        return ResultUtils.error(e.getCode(), e.getMessage(), e.getDescription());
    }

    /**
     * 其他异常处理器
     */
    @ExceptionHandler(Exception.class)
    public BaseResponse exceptionHandler(Exception e) {
        log.error("Exception:", e);
        return ResultUtils.error(ErrorCode.SYS_ERROR, e.getMessage(), "未知异常");
    }
}
